package utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class LessonsTasks extends AsyncTask<String, String, String> {

	JSONParser jsonParser = new JSONParser();
	JSONArray categories = null;
	JSONArray lessons = null;
	JSONArray contents = null;

	private final String LESSONS_LIST = "<li><a %EVENT%>%LESSON%</a></li>";
	private final String CHAPTERS_LIST = "<div data-role='collapsible'><h3>%CHAPTER%</h3><ul data-role='listview'>%LESSONS%</ul></div>";
	
	private final String SUBCHAPTERS_LIST = "<div data-role='collapsible' data-collapsed='false' data-theme='b'><h3>%SUBCHAPTER%</h3><p>%CONTENT%</p></div>";
	
	private String url_get_categories = "http://weblabtutor.comyr.com/get_all_categories.php";
	private String url_get_lessons_by_category = "http://weblabtutor.comyr.com/get_lessons_by_category.php";
	private String url_get_lessons_content = "http://weblabtutor.comyr.com/get_content_by_lesson.php";

	private final String TAG_SUCCESS = "success";
	private final String TAG_CATEGORIES = "categories";
	private final String TAG_ID = "id";
	private final String TAG_NAME = "name";
	private final String TAG_LESSONS = "lessons";
	private final String TAG_TEXT = "text";
	private final String TAG_CONTENTS = "contents";

	protected String doInBackground(String... args) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		String output = CHAPTERS_LIST;
		String contentOutput = SUBCHAPTERS_LIST;
		String finalOutput = "";
		String lessonOutput = "";
		
		if (args[0].equalsIgnoreCase("get_categories")) {
			JSONObject json = jsonParser.makeHttpRequest(url_get_categories, "GET", params);

			Log.d("All Categories: ", json.toString());

			try {
				int success = json.getInt(TAG_SUCCESS);
				if (success == 1) {
					categories = json.getJSONArray(TAG_CATEGORIES);

					for (int i = 0; i < categories.length(); i++) {
						JSONObject c = categories.getJSONObject(i);

						String id = c.getString(TAG_ID);
						String name = c.getString(TAG_NAME);

						finalOutput += output;
						finalOutput = finalOutput.replace("%CHAPTER%", name);
						
						params.clear();
						params.add(new BasicNameValuePair("category_id", id));
						JSONObject lesson = jsonParser.makeHttpRequest(url_get_lessons_by_category,
								"GET", params);
						int success1 = lesson.getInt(TAG_SUCCESS);
						if (success1 == 1) {
							lessons = lesson.getJSONArray(TAG_LESSONS);

							for (int j = 0; j < lessons.length(); j++) {
								JSONObject obj = lessons.getJSONObject(j);
								String lessonName = obj.getString(TAG_NAME);
								int lesson_id = obj.getInt(TAG_ID);
								lessonOutput += LESSONS_LIST;
								lessonOutput = lessonOutput.replace("%LESSON%", lessonName);
								lessonOutput = lessonOutput.replace("%EVENT%", "onclick=\"return viewLessonContent("+lesson_id+");\"");
							}
						}
						finalOutput = finalOutput.replace("%LESSONS%", lessonOutput);
						lessonOutput = "";
					}
					return finalOutput;
				} else {
					Toast.makeText(null, "Error", Toast.LENGTH_LONG);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		else
			if(args[0].equalsIgnoreCase("get_content")){
				String lesson_id = args[1];
				params.clear();
				params.add(new BasicNameValuePair("lesson_id", lesson_id));
				JSONObject json = jsonParser.makeHttpRequest(url_get_lessons_content, "GET", params);

				Log.d("All Content: ", json.toString());

				try {
					int success = json.getInt(TAG_SUCCESS);
					if (success == 1) {
						contents = json.getJSONArray(TAG_CONTENTS);

						for (int i = 0; i < contents.length(); i++) {
							JSONObject c = contents.getJSONObject(i);
							String name = c.getString(TAG_NAME);
							String text = c.getString(TAG_TEXT);
							finalOutput += contentOutput;
							finalOutput = finalOutput.replace("%SUBCHAPTER%", name);
							finalOutput = finalOutput.replace("%CONTENT%", text);
						}
						return finalOutput;
					} else {
						Toast.makeText(null, "Error", Toast.LENGTH_LONG);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		return finalOutput;
	}
}
